using System;
using System.Text;
using System.Data;
using System.Collections;
using System.Collections.Generic;

using Framework;

namespace SchemaMonitor
{
	//Table-Mapping Class (Customisable half)
	public partial class CMetaTag
	{
		#region Constants
		#endregion

		#region Constructors (Public)
		//Default Connection String
		public CMetaTag() : base() { }

		//Alternative Connection String
		public CMetaTag(CDataSrc dataSrc) : base(dataSrc) { }

		//Hidden  (UI code should use cache instead)
		protected internal CMetaTag(int tagId) : base(tagId) { }
		protected internal CMetaTag(CDataSrc dataSrc, int tagId) : base(dataSrc, tagId) { }
		protected internal CMetaTag(CDataSrc dataSrc, int tagId, IDbTransaction txOrNull) : base(dataSrc, tagId, txOrNull) { }
		#endregion

		#region Default Values
		protected override void InitValues_Custom()
		{
			//_sampleDateCreated = DateTime.Now;
			//_sampleSortOrder   = 0;
		}
		#endregion

		#region Default Connection String
		protected override CDataSrc DefaultDataSrc() { return CConfig.Photos; } //CDataSrc.Default;  }
		#endregion

		#region Properties - Relationships
		//Relationships - Foriegn Keys (e.g parent)
		public CTagType TagType { get { return CTagType.Cache.GetById(this.TagTypeId); } }

		//Relationships - Collections (e.g. children)
		public CLabelList Labels { get { return CLabel.Cache.GetByTagId(this.TagId); } }
		protected CLabelList Labels_(IDbTransaction tx) { return new CLabel(DataSrc).SelectByTagId(this.TagId, tx); } //Only used for cascade deletes

		public CLabel_OneToOneList Label_OneToOnes { get { return CLabel_OneToOne.Cache.GetByTagId(this.TagId); } }
		protected CLabel_OneToOneList Label_OneToOnes_(IDbTransaction tx) { return new CLabel_OneToOne(DataSrc).SelectByTagId(this.TagId, tx); } //Only used for cascade deletes

		public CNodeLabelList NodeLabels { get { return CNodeLabel.Cache.GetByTagId(this.TagId); } }
		public CNodeLabels_OneToOneList NodeLabels_OneToOne { get { return CNodeLabels_OneToOne.Cache.GetByTagId(this.TagId); } }

		//Relationships - 2-Step Walk
		public CNodeList Nodes { get { return NodeLabels.Nodes; } }
		public CNodeList NodesO2O { get { return NodeLabels_OneToOne.Nodes; } }

		public CChapterList Chapters { get { return CChapter.Cache.GetByNodeGuids(NodeLabels.NodeIds); } }
		public CChapterList ChaptersO2O { get { return CChapter.Cache.GetByNodeGuids(NodeLabels_OneToOne.NodeIds); } }

		public CPhotoHashList PhotoHashs { get { return Labels.PhotoHashs; } }
		public CPhotoHashList PhotoHashsO2O { get { return Label_OneToOnes.PhotoHashs; } }

		public CPhotoHashList RemainingPhotoHashs(string search) { return Labels.RemainingPhotoHashs(search); }
		public CPhotoHashList RemainingPhotoHashsO2O(string search) { return Label_OneToOnes.RemainingPhotoHashs(search); }
		#endregion

		#region Properties - Customisation
		//Derived/ReadOnly (e.g. xml classes, presentation logic)
		public string NameAndCount //image count
		{
			get
			{
				return CUtilities.NameAndCount(TagName, TagType.TagIsOneToOne ? Label_OneToOnes.Count : Labels.Count);
			}
		}
		public string NameAndNodeCount
		{
			get
			{
				return CUtilities.NameAndCount(TagName, TagType.TagIsOneToOne ? NodeLabels_OneToOne.Count : NodeLabels.Count);
			}
		}
		public string FullName
		{
			get
			{
				return string.Concat(TagType.FullName, " :: ", NameAndCount); 
			}
		}

		#endregion

		#region Save/Delete Overrides
		//Can Override base.Save/Delete (e.g. Cascade deletes, or insert related records)
		#endregion

		#region Custom Database Queries
		//(Not normally required for cached classes, use list class for searching etc)
		//For Stored Procs can use: MakeList (matching schema), or DataSrc.ExecuteDataset (reports etc)
		//For Dynamic sql, can use: SelectSum, SelectDistinct, SelectCount, SelectWhere (inherited methods)
		#endregion

		#region Searching (Optional)
		//For cached classes, custom seach logic resides in static methods on the list class
		// e.g. CMetaTag.Cache.Search("...")

		//See also the auto-generated methods based on indexes
		//' e.g. CMetaTag.Cache.GetBy...
		#endregion

		#region Caching Details
		//Cache Key
		internal static string CACHE_KEY = typeof(CMetaTag).ToString();    //TABLE_NAME

        //Cache data
        private static CMetaTagList LoadCache()  {   return new CMetaTag().SelectAll();   }
        //Cache Timeout
        private static void SetCache(CMetaTagList value)
        {
            if (null != value)  
                value.Sort(); 
            CCache.Set(CACHE_KEY, value);    //Optional parameter can override timeout (otherwise uses config-settings, which default to 3hrs)
        }
        //Helper Method
        private CMetaTag CacheGetById(CMetaTagList list)  { return list.GetById(this.TagId);    }
        #endregion

        #region Cloning
        public CMetaTag Clone(CDataSrc target, IDbTransaction txOrNull) //, int parentId)
        {
            //Shallow copy: Copies the immediate record, excluding autogenerated Pks
            CMetaTag copy = new CMetaTag(this, target);

            //Deep Copy - Child Entities: Cloned children must reference their cloned parent
            //copy.SampleParentId = parentId;

            copy.Save(txOrNull);

            //Deep Copy - Parent Entities: Cloned parents also clone their child collections
            //this.Children.Clone(target, txOrNull, copy.TagId);

            return copy;
        }
        #endregion

        #region ToXml
        protected override void ToXml_Custom(System.Xml.XmlWriter w)
        {
            //Store(w, "Example", this.Example)
        }
        #endregion
    }
}